<template>
<div>
	<ul class="list">
		<li class="item" v-for='(item) of letters' :key=item
		@click='handleLetterClick'
		@touchstart.prevent='handleTouchStart'
		@touchmove='handleTouchMove'
		@touchend='handleTouchEnd'
		:ref = item
		>{{item}}</li>
	</ul>
</div>
</template>
<script>
	export default{
		name:'CityAlphabet',
		props:{
			cities:{
				type:Object,
				required:true
			}
		},
		data(){
			return {
				touchStatus:false,
				timer:null,
				startY:0
			}
		},
		updated(){
			this.startY = this.$refs['A'][0].offsetTop 
		},
		computed:{
			letters(){
				const letters = []
				for(let i in this.cities){
					letters.push(i)
				}
				return letters
			}
		},
		methods:{
			handleLetterClick(e){
				this.$emit('change',e.target.innerText)
			},
			handleTouchStart(){
				this.touchStatus = true
			},
			handleTouchMove(e){
				if(this.touchStatus){
					if(this.timer){
						clearTimeout(this.timer)
					}
					this.timer = setTimeout(()=>{
						//this.startY获取A距离父类顶部的距离。
						const touchY = e.touches[0].clientY - 65
						//摁下的地方减去头部距离。但是这个时候并没有减去这个列表第一个数字和父类顶部的距离
						const index = Math.floor((touchY - this.startY)/20)
						//用摁下的地方减去A距离父类顶部距离/20 得出来手指所在地方的下标。
						console.log(index)
						if(index>=0&&index<this.letters.length){
							this.$emit('change',this.letters[index])
						}
					},16)
				}
			},
			handleTouchEnd(){
				this.touchStatus = false
			}
		}
	}
</script>
<style scoped lang="stylus">
@import '~@/assets/styles/global.styl'
	.list{
		position: absolute;
		display: flex;
		flex-direction: column;
		justify-content: center;
		right: 0;
		bottom: 0;
		top: 1.86rem;
		width: .4rem;
		font-size: .28rem;
		z-index: 2;
	}
	.item{
		line-height: .4rem;
		text-align: center;
		color: $bgColor
	}
</style>